package com.panomedic.log4j;

import com.panomedic.Constants;
import com.panomedic.utils.SystemProperties;
import java.io.File;
import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.util.Properties;
import java.util.Enumeration;
import java.util.PropertyResourceBundle;

public class Log4jPropertiesLoader {

    /**
    * Set log4j properties
    * @param multipleLog
    */
    public static void loadProperties(boolean debug) {
        PropertyResourceBundle bundle;
        try 
        {
            bundle = new PropertyResourceBundle(new FileInputStream(Constants.LOG_PROPERTIES_FILE));
            Enumeration<String> keys = bundle.getKeys();
            Properties props = new Properties();
            
            while (keys.hasMoreElements()) 
            {
                String key = keys.nextElement();
                String value = bundle.getString(key);

                // Change to DEBUG MODE if debug
                if (key.equals("log4j.rootLogger") && debug) 
                {
                    value = value.replace("INFO", "DEBUG");
                } else if (key.equals("log4j.appender.A2.file")) {
//                    value = SystemProperties.getUserConfigFolder(debug) + SystemProperties.fileSeparator + Constants.LOG_OUTPUT_FILE;
                    value = new File("").getAbsolutePath() + SystemProperties.fileSeparator + Constants.LOG_OUTPUT_FILE;
                }

                props.setProperty(key, value);
            }
            
            PropertyConfigurator.configure(props);
    
        } catch (Exception e) {
            System.out.println("ERROR trying to read logger configuration");
        }
    }
    
}
